class createRoute {
  constructor (name, path, component) {
    this.name = name
    this.component = component
    this.path = path
    this.children = []
  }

  getFullPath (route) {
    let path = route.path
    if (/!^\//.test(path)) {
      path = `/${path}`
    }
    path.replace(/\/$/, '')
    if (route.parentRoute) {
      // console.log('----->', this.getFullPath(route.parentRoute) + path)
      return (this.getFullPath(route.parentRoute) + path).replace('//', '/')
    }
    return path
  }

  addChild (route) {
    route.parentRoute = this
    route.fullpath = this.getFullPath(route)
    if(!this.children.length){
      this.children.push(route)
      return this
    }

    this.children.forEach((v,i)=>{
      if(v.path === route.path){
        throw new Error('路由名字必须是唯一的')
      }
    })
    this.children.push(route)
    return this
  }

}
export default function(name,path,component){
  return new createRoute(name,path,component)
}
